package com.ucap.bbs.controller.action;

import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.ucap.bbs.pojo.Module;
import com.ucap.bbs.pojo.Topic;
import com.ucap.bbs.pojo.Users;
import com.ucap.bbs.service.IModuleService;
import com.ucap.bbs.service.ITopicService;
import com.ucap.bbs.util.ConstantUtil;
import com.ucap.bbs.util.GUID;
import com.ucap.bbs.util.StringUtils;
import com.ucap.bbs.util.bean.MessageInfoBean;

public class TopicAction extends ActionSupport {
	private ITopicService topicService;
	private IModuleService moduleService;
	
	public void setModuleService(IModuleService moduleService) {
		this.moduleService = moduleService;
	}

	public void setTopicService(ITopicService topicService) {
		this.topicService = topicService;
	}

	public String queryListForIndexZxhf(){
		Topic queryTopic =  new Topic();
		queryTopic.setEndLimit(13);
		queryTopic.setBmReplyStatus(1);
		List<Topic> list = this.topicService.queryListForIndexZxhf(queryTopic);
		ServletActionContext.getRequest().setAttribute("list", list);
		return "zxhf";
	}
	
	public String queryListForIndexZxft(){
		Topic queryTopic =  new Topic();
		queryTopic.setEndLimit(13);
		List<Topic> list = this.topicService.queryTopicListForPage(queryTopic);
		ServletActionContext.getRequest().setAttribute("list", list);
		return "zxft";
	}
	
	public String queryListForIndexRdwt(){
		Topic queryTopic =  new Topic();
		queryTopic.setEndLimit(6);
		List<Topic> list = this.topicService.queryTopicListForRdwt(queryTopic);
		ServletActionContext.getRequest().setAttribute("list", list);
		return "rdwt";
	}
	
	public String queryListForIndexBmhfltj(){
		List<Map> list = this.topicService.queryListForIndexBmhfltj();
		ServletActionContext.getRequest().setAttribute("list", list);
		return "bmhfltj";
	}
	
	/**
	 * 新增发帖
	 * @return
	 */
	public String addTopic(){
		Users loginUser = (Users)ActionContext.getContext().getSession().get(ConstantUtil.LOGIN_USER_BEAN);
		if(null!=loginUser){
			topic.setUserGuid(loginUser.getGuid());
		}else{
			messageInfoBean.addMsg("必须要登录才能发帖！");
			messageInfoBean.setIsRedirect(true);
			messageInfoBean.setRedirect("login.jsp");
			ServletActionContext.getRequest().setAttribute("messageInfoBean", messageInfoBean);
			return "msg";
		}
		
		//判断非空
		if(StringUtils.isEmpty(topic.getTitle()) || StringUtils.isEmpty(topic.getContent())){
			messageInfoBean.addMsg("标题和内容不能为空！");
			ServletActionContext.getRequest().setAttribute("messageInfoBean", messageInfoBean);
			return "msg";
		}
		topic.setGuid(GUID.getGUID());
		this.topicService.addTopic(topic);
		messageInfoBean.addMsg("发帖成功");
		messageInfoBean.setIsRedirect(true);
		messageInfoBean.setRedirect("topic/addTopic.do?module="+topic.getModuleGuid());
		ServletActionContext.getRequest().setAttribute("messageInfoBean", messageInfoBean);
		return "msg";
	}
	
	public String queryTopicList(){
		String moduleGuid = ServletActionContext.getRequest().getParameter("module");//获取模块
		//查询模块信息
		
		Module module = this.moduleService.getModuleDetail(moduleGuid);
		ServletActionContext.getRequest().setAttribute("module", module);
		topic.setModuleGuid(moduleGuid);
		List<Topic> list = this.topicService.queryTopicList(topic);
		ServletActionContext.getRequest().setAttribute("list", list);
		return "topic_list";
	}
	
	public String queryTopicDetail(){
		String guid = ServletActionContext.getRequest().getParameter("guid");
		Topic topic = this.topicService.queryTopicDetail(guid);
		System.out.println(topic);
		this.setTopic(topic);
		return "topic_detail";
	}
	
	
	private MessageInfoBean messageInfoBean = new MessageInfoBean();
	
	public MessageInfoBean getMessageInfoBean() {
		return messageInfoBean;
	}

	public void setMessageInfoBean(MessageInfoBean messageInfoBean) {
		this.messageInfoBean = messageInfoBean;
	}
	
	private Topic topic = new Topic();

	public Topic getTopic() {
		return topic;
	}

	public void setTopic(Topic topic) {
		this.topic = topic;
	}
}
